package com.ustc.hewei.snowflakemodule;

import com.ustc.hewei.snowflakemodule.segment.SegmentWorker;
import com.ustc.hewei.snowflakemodule.snowFlake.BenchmarkSnowflake;
import com.ustc.hewei.snowflakemodule.snowFlake.SnowflakeImpl;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * @author hewei
 * @version 1.0
 * @description: segment测试类
 * @date 2022/11/17 09:41
 */

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Threads(5)
public class BenchmarkSegment {

    private SegmentWorker test1;

    @org.openjdk.jmh.annotations.Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public Long test() {
        Long temp = test1.getId("test").getId();
        System.out.println(temp);
        return temp;
    }

    public static void main(String[] args) throws Exception {
        // 使用一个单独进程执行测试，执行5遍warmup，然后执行3遍测试
        Options opt = new OptionsBuilder().include(BenchmarkSegment.class.getSimpleName()).forks(1).warmupIterations(5)
                .measurementIterations(3).output("/Users/hewei/java-project/myMiaoshaWithRpc/snowFlakemodule/src/main/resources/Benchmark-segment-mutilThreads.json").build();
        new Runner(opt).run();
    }

    @Setup
    public void init() {
        ConfigurableApplicationContext context = SpringApplication.run(SnowFlakemoduleApplication.class);
        test1 = context.getBean(SegmentWorker.class);
        //est1.init();
    }
}
